<?php

namespace app\admin\controller;

use app\common\model\Setting as ModelSetting;
use think\App;

class Setting extends Base
{
    protected $setting;

    public function __construct(App $app, \app\common\model\Setting $setting)
    {
        parent::__construct($app);
        $this->setting = $setting;
    }

    public function index()
    {
        if (isAjax()) {
            $result = $this->setting->select();
            $list = [];
            foreach ($result as $value) {
                $list[$value["key"]] = $value['content'];
            }
            ajaxSuccess($list);
        }
        return $this->fetch();
    }

    public function save()
    {
        $key = input('post.key');
        $content = json_decode(input('post.content'), true);
        $setting = $this->setting->where('key', $key)->find();
        $setting->content = $content;
        $setting->save();
        ajaxSuccess();

    }
}